<?php
namespace Core;
use \GatewayWorker\Lib\Db;
use \GatewayWorker\Lib\Gateway;
//服务端
class ApiMessage{

    //获取店铺临时聊天列表
    public static function get_friends($shop_id){
        $sql=<<<EFO
SELECT f.shop_id,f.user_id,uu.nickname,uu.avatar,uu.phone,coalesce(message.count,0) as count FROM `b_shop_message_friends` as f
left join f_users as uu on f.user_id=uu.id
left join (select count(accept_id) as count,accept_id,send_id from b_shop_message where is_read=0 group by send_id) as message on message.send_id=f.user_id
where f.shop_id={$shop_id} and f.is_delete=0 order by count desc;
EFO;
        $db1 = Db::instance('db')->query($sql);
        foreach($db1 as $key=>$value){
            $db1[$key]['is_online']= Gateway::isUidOnline($value['user_id']);
            $db1[$key]['nickname']=empty($value['nickname']) ? substr_replace($value['phone'],'****',3,4) : $value['nickname'];
            $db1[$key]['avatar']=empty($value['avatar']) ? 'http://qn.enshitc.com/buygou1.png' : $value['avatar'];
        }
        return self::json_return('friends',$db1,'获取好友列表成功');
    }

    //查询用户的聊天记录
    public static function message_list($data){
        $sqlcount=<<<EFO
SELECT count(id) as `count` from b_shop_message where send_id={$data['user_id']} and accept_id={$data['shop_id']} and is_read=0
EFO;
        $user_count_message = Db::instance('db')->single($sqlcount);
        if($user_count_message){
            Db::instance('db')->update('b_shop_message')->cols(array('is_read'=>'1'))->where(" send_id={$data['user_id']} and accept_id={$data['shop_id']} and is_read=0")->query();
        }
        $sql=<<<EFO
SELECT m.id,m.send_id as send_user_id,m.shop_id,m.user_id,m.created_at,m.message as content,m.type,m.extends,m.msg_type,u.nickname,u.phone,u.avatar,su.username as shop_nickname,su.avatar as shop_avatar FROM `b_shop_message` as m
left join f_users as u on u.id=m.user_id
left join b_shop_users as su on su.id=m.shop_id
where m.shop_id={$data['shop_id']} and m.user_id={$data['user_id']}
ORDER BY m.created_at asc
EFO;
        $db1 = Db::instance('db')->query($sql);
        foreach($db1 as $key=>$value){
            $db1[$key]['nickname']=empty($value['nickname']) ? substr_replace($value['phone'],'****',3,4) : $value['nickname'];
            $db1[$key]['avatar']=empty($value['avatar']) ? 'http://qn.enshitc.com/buygou1.png' : $value['avatar'];
            $db1[$key]['created_at'] = self::format_date($value['created_at']);
        }
        return self::json_return('api_message_list',$db1,'获取好友消息成功');
    }


    //插入用户发送的消息
    public static function insert_message($data){
        $current_time=date('Y-m-d H:i:s');
        $insert_id = Db::instance('db')
            ->insert('b_shop_message')
            ->cols(
                array(
                    'shop_id'=>$data['shop_id'],
                    'user_id'=>$data['user_id'],
                    'message'=>$data['content'],
                    'created_at'=>$current_time,
                    'updated_at'=>$current_time,
                    'is_read'=>0,
                    'send_id'=>$data['send_user_id'],
                    'accept_id'=>$data['accept_id'],
                    'type'=>$data['type'],
                    'extends'=>isset($data['extends']) ? $data['extends'] :'',
                )
            )
            ->query();

        $sql_one=<<<EFO
SELECT m.id,m.send_id as send_user_id,m.shop_id,m.user_id,m.created_at,m.message as content,m.type,m.extends,m.msg_type,u.nickname,u.phone,u.avatar,su.username as shop_nickname,su.avatar as shop_avatar FROM `b_shop_message` as m
left join f_users as u on u.id=m.user_id
left join b_shop_users as su on su.id=m.shop_id
where m.id={$insert_id}
ORDER BY m.created_at asc
EFO;
        $db1 = Db::instance('db')->query($sql_one);
        foreach($db1 as $key=>$value){
            $db1[$key]['nickname']=empty($value['nickname']) ? substr_replace($value['phone'],'****',3,4) : $value['nickname'];
            $db1[$key]['avatar']=empty($value['avatar']) ? 'http://qn.enshitc.com/buygou1.png' : $value['avatar'];
            $db1[$key]['created_at'] = self::format_date($value['created_at']);
        }
        return self::json_return('api_message',$db1[0],'获取好友消息成功');
    }


    /**
     * 数据返回
     */
    public static function json_return($type,$data,$msg){
        $result['type'] = $type;
        $result['data']=$data;
        $result['msg']=$msg;
        return json_encode($result);
    }


    /**
     * 计算两个时间之差
     * @param $time
     * @return string
     */
    public static function format_date($date){
        $time = strtotime($date);
        $t=time()-$time;
        $f=array(
            '31536000'=>'年',
            '2592000'=>'个月',
            '604800'=>'星期',
            '86400'=>'天',
            '3600'=>'小时',
            '60'=>'分钟',
            '1'=>'秒'
        );
        foreach ($f as $k=>$v)    {
            if (0 !=$c=floor($t/(int)$k)) {
                return $c.$v.'前';
            }
        }
    }
}